package com.jdsh.encrypt.controller;

import com.alibaba.fastjson.JSONObject;
import com.jdsh.encrypt.entity.CryptoDecryptParam;
import com.jdsh.encrypt.entity.CryptoEncryptParam;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

import static com.jdsh.encrypt.utils.Sm2Util.*;

@RestController
@RequestMapping("")
public class CryptoController {

    @PostMapping("/Encrypt")
    public String Encrypt(@RequestBody CryptoEncryptParam param) {
        try {
            String data = param.getData();
            JSONObject Result = JSONObject.parseObject(encrypted(data));
            return JSONObject.toJSONString(Result);
        } catch (UnsupportedEncodingException e) {
            return "加密失败";
        }
    }


    @PostMapping("/EncryptS")
    public String EncryptS(@RequestBody CryptoEncryptParam param) {
        try {
            String data = param.getData();

            JSONObject Result = JSONObject.parseObject(encryptedS(data));


            return JSONObject.toJSONString(Result);
        } catch (UnsupportedEncodingException e) {
            return "加密失败";
        }
    }

    @PostMapping("/DecryptA")
    public String Decrypt1(@RequestBody CryptoDecryptParam param) {
        try {
            String jsonString = JSONObject.toJSONString(param);
            String Result = decryptedA(jsonString);
//            Result = "{\"result\":\"" + Result + "\"}";
            return Result;
        } catch (Exception e) {
            return "解密失败";
        }
    }

    @PostMapping("/DecryptB")
    public String Decrypt2(@RequestBody CryptoDecryptParam param) {
        try {
            String jsonString = JSONObject.toJSONString(param);
            String Result = decryptedB(jsonString);
//            Result = "{\"result\":\"" + Result + "\"}";
            return Result;
        } catch (Exception e) {
            return "解密失败";
        }
    }
}
